package com.vn.ducktohome.screen.play;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.vn.ducktohome.constant.Constant;

public class LevelHelper {
	
	private static FileHandle fileHandle, fileReadLevel;
	private static String c[];
	
	/**
	 * doc file, luu vao c[]
	 * @param level
	 */
	private static void readFile(){
		fileHandle = Gdx.files.internal("GameConfig/list_level.txt");
		String content = fileHandle.readString();
		c = content.split("_#");
		Constant.MAX_LEVEL = c.length;
		Gdx.app.log("levelhelper - max level", String.valueOf(Constant.MAX_LEVEL));
	}
	
	/**
	 * tra ve string cua level, mô tả thông tin cá sấu của level.
	 * @param iLevel
	 * @return
	 */
	public static String readLevel(int iLevel){
		if (c == null)
			readFile();
		if (iLevel > c.length)
			return null;
		return c[iLevel - 1];
	}
	
	/**
	 * đọc level hiện tại.
	 */
	public static void readLevel(){
		if (fileReadLevel == null){
			fileReadLevel = Gdx.files.internal("GameConfig/level.txt");
			String content[] = fileReadLevel.readString().split("_");
			Constant.LEVEL = Integer.valueOf(content[0]);
			Gdx.app.log("LevelHelper - level", String.valueOf(Constant.LEVEL));
			Constant.LIVES = Integer.valueOf(content[1]);
			Gdx.app.log("LevelHelper - lives", String.valueOf(Constant.LIVES));
		}
	}
	
	/**
	 * Ghi level đã đạt được vào file.
	 */
	public static void writeLevel(){
		fileReadLevel = Gdx.files.local("GameConfig/level.txt");
		fileReadLevel.writeString(String.valueOf(Constant.LEVEL), false);
		fileReadLevel.writeString("_", true);
		fileReadLevel.writeString(String.valueOf(Constant.LIVES), true);
	}
	
	public static void resetLevel(){
		Constant.LIVES = 3;
		Constant.ACTIVE_LEVEL = 1;
		Constant.LEVEL = 1;
		fileReadLevel = null;
		fileReadLevel = Gdx.files.local("GameConfig/level.txt");
		fileReadLevel.writeString("1_3", false);
	}
}